package com.xs.ahoi.hcxs.ui.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.view.WindowManager;
import com.githang.statusbar.StatusBarCompat;
import com.xs.ahoi.hcxs.Ahoi;
import com.xs.ahoi.hcxs.R;
import com.xs.ahoi.hcxs.dao.ConfigDao;
import com.xs.ahoi.hcxs.utils.LogUtil;
import com.xs.ahoi.hcxs.utils.ToastUtil;

public class BaseActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(this, R.color.colorBlack), false);

        Ahoi.getInstance().addOne(this);
    }

    // 隐藏状态栏
    public void hideStatusBar() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    @Override
    protected void onDestroy() {
        Ahoi.getInstance().removeOne(this);
        super.onDestroy();
    }

    /**
     * 销毁所有的Activity
     */
    public void removeAll() {
        Ahoi.getInstance().removeAll();
    }

    /**
     * 获取图片名称获取图片的资源id的方法
     */
    public int getResource(String resourceName, String resourceType) {
        return getResources().getIdentifier(resourceName, resourceType, getBaseContext().getPackageName());
    }

    public enum ResourceType {
        drawable, id, layout
    }

    public void startFloatWinsService(Intent intent) {
        try {
            if (!ConfigDao.getInstance().getFloatWins()) {
                return;
            }
            if (!Settings.canDrawOverlays(this)) {
                ToastUtil.show("无悬浮窗权限");
                startActivityForResult(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName())), 0);
            } else {
                startService(intent);
            }
        } catch (Exception e) {
            LogUtil.e("启动悬浮窗service失败：" + e.getMessage(), e);
        }

    }

    public void stopFloatWinsService(Intent intent) {
        try {
            stopService(intent);
        } catch (Exception ignored) {
        }
    }
}